<?php
namespace Admin\Controller;
use Think\Controller;
class PrinterController extends CommonController {
	//封装打印信息
	//打印入库单、出库单
	// **********
	// order------基础表名
	// orderdetail-----详情表名
	// id----基础表行id
	// inbound-----判断是否入库，为1:入库
	// outbound------判断是否出库，为1：出库
	// **********
	public function orders(){
		$order=I('order');//订单(制单)表
		$orderdetail=I('orderdetail');//订单(制单)详情表
		$id=I('id');//订单(制单)表数据字段id的值
		$inbound=I('inbound');//入库
		$outbound=I('outbound');//出库
		$detail=M($order,C('DB_PRS'),C('DB_DEPOT'))->where("id=$id")->find();
		
		$fields = M($orderdetail,C('DB_PRS'),C('DB_DEPOT'))->getDbFields();//取出模型字段，返回数组
		$status=in_array('plan_id',$fields);//判断字段plan_id是否存在
		
		if($status){
			$list=M($orderdetail,C('DB_PRS'),C('DB_DEPOT'))->where("plan_id=$id")->select();
		}else{
			$list=M($orderdetail,C('DB_PRS'),C('DB_DEPOT'))->where("order_id=$id")->select();
		}
		foreach($list as $k=>$v){
			if($v['section_no']){
				$list[$k]['section_name']=M('Section',C('DB_PRS'),C('DB_DEPOT'))->where(array('no'=>$v['section_no']))->find();
			}
			if($v['contacts_company_no']){
				$list[$k]['company_name']=M('ContactsCompany',C('DB_PRS'),C('DB_DEPOT'))->where(array('no'=>$v['contacts_company_no']))->find();//往来单位
			}
			if($v['people_make_no']){
				$list[$k]['people_make_name']=M('Personnel',C('DB_PRS'),C('DB_DEPOT'))->where(array('no'=>$v['people_make_no']))->find();//制单人
			}
			if($v['people_purchasing_no']){
				$list[$k]['people_purchasing_name']=M('Personnel',C('DB_PRS'),C('DB_DEPOT'))->where(array('no'=>$v['people_purchasing_no']))->find();//采购申请人
			}
			if($v['auditor']){
				$list[$k]['auditor_name']=M('User',C('DB_PRS'),C('DB_DEPOT'))->where(array('username'=>$v['auditor']))->find();//审核人
			}
			
			$coin_model = M('coin',C('DB_PRS'),C('DB_DEPOT'));
            $unit_model =  M('unit',C('DB_PRS'),C('DB_DEPOT'));
			if($v['current_type']){
				$current_type = $coin_model->where(array('coin_code'=>$v['current_type']))->find();
				$list[$k]['coin_name']   =  $current_type['coin_name'];
			}
			if($v['declare_unit']){
				$unit = $unit_model->where(array('unit_no'=>$v['declare_unit']))->find();
				$list[$k]['unit_name']   =  $unit['unit_name'];
			}
            if($v['unit1']){
                $unit1 = $unit_model->where(array('unit_no'=>$v['unit1']))->find();
				$list[$k]['unit1_name']  =  $unit1['unit_name'];
            }

            if($v['unit2']){
                $unit2 = $unit_model->where(array('unit_no'=>$v['unit2']))->find();
				$list[$k]['unit2_name']  =  $unit2['unit_name'];
            }
			$status_two=in_array('goods_name',$fields);//判断字段goods_name是否存在
			
			if(!$status_two){
				
				if($order=='PurchasingOrder'){
					$Goods=M('PurchasingGoods',C('DB_PRS'),C('DB_DEPOT'));//设置表名
				}
				if($order=='ProductOrder' or $order=='SellOrder'){
					$Goods=M('PurchasingInboundGoods',C('DB_PRS'),C('DB_DEPOT'));//设置表名
				}
				if($order=='PushOrder' or $order=='Sampling'){
					$Goods=M('Goods',C('DB_PRS'),C('DB_DEPOT'));//设置表名
				}
				
				if($v['goods_sn']){
					$goods_name=$Goods->where(array('goods_no'=>$v['goods_sn']))->find();
				}else{
					$goods_name=$Goods->where(array('goods_no'=>$v['goods_no']))->find();
				}
				$list[$k]['goods_name']=$goods_name['goods_name'];
			}
			
		}
		
		$this->assign('detail',$detail);
		$this->assign('list',$list);
		if($inbound){
			$this->display('inbound');
		}
		if($outbound){
			$this->display('outbound');
		}
	}
}